package com.davidrubio.main;

import in.wptrafficanalyzer.actionbarnavtab.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	/** Called when the activity is first created. */
	public static Context appContext;
	public static int tabActive;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		appContext = getApplicationContext();

		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.setDisplayShowTitleEnabled(false);

		Tab tab = actionBar
				.newTab()
				.setText(R.string.gastos)
				.setTabListener(
						new CustomTabListener<ExpensesFragment>(this, "gastos",
								ExpensesFragment.class));
		// .setIcon(R.drawable.android);

		actionBar.addTab(tab);

		tab = actionBar
				.newTab()
				.setText(R.string.ingresos)
				.setTabListener(
						new CustomTabListener<IncomesFragment>(this,
								"ingresos", IncomesFragment.class));
		// .setIcon(R.drawable.apple);

		actionBar.addTab(tab);

		tab = actionBar
				.newTab()
				.setText(R.string.resumen)
				.setTabListener(
						new CustomTabListener<SumupFragment>(this, "resumen",
								SumupFragment.class));
		// .setIcon(R.drawable.apple);

		actionBar.addTab(tab);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuitem_search:
			Toast.makeText(appContext, "Search", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_add:
			// ExpensesFragment.AddItem("A");
			add();
			Toast.makeText(appContext, "Add", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_feedback:
			Toast.makeText(appContext, "Feedback", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_about:
			Toast.makeText(appContext, "About", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_quit:
			Toast.makeText(appContext, "Quit", Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
	}

	private void add() {

		final View addView = getLayoutInflater().inflate(R.layout.add, null);
		// final TextView lblMensaje = (TextView) findViewById(R.id.LblMensaje);
		Spinner spinner = (Spinner) addView.findViewById(R.id.repeatlist);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.repeatlist_array,
				android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);

		String title = new String();
		if (tabActive == 0) {
			title = "Nuevo gasto";
		} else {
			title = "Nuevo ingreso";
		}
		new AlertDialog.Builder(this).setTitle(title).setView(addView)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						TextView tv1 = (TextView) addView
								.findViewById(R.id.add_fecha);
						TextView tv2 = (TextView) addView
								.findViewById(R.id.add_cant);
						addWord(tv1.getText().toString(), tv2.getText()
								.toString());
					}
				}).setNegativeButton("Cancel", null).show();
	}

	private void addWord(String date, String money) {
		if (tabActive == 0) {
			ExpensesFragment.addItem(date, money);
		} else {
			IncomesFragment.addItem(date, money);
		}
		Toast.makeText(appContext, date + "-" + money + " is Entered",
				Toast.LENGTH_LONG).show();
		//
		// title.setText("");
	}

	public void showDatePickerDialog(View v) {
		EditText textfecha = (EditText) v.findViewById(R.id.add_fecha);
		DatePickerFragment newFragment = new DatePickerFragment(textfecha);
		newFragment.show(getFragmentManager(), "datePicker");
	}
	// private TextView.OnEditorActionListener onSearch = new
	// TextView.OnEditorActionListener() {
	// public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
	// if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
	// addWord(v);
	//
	// InputMethodManager imm = (InputMethodManager)
	// getSystemService(INPUT_METHOD_SERVICE);
	//
	// imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	// }
	//
	// return (true);
	// }
	// };
}